﻿using System.IO;
using System.Web.Mvc;
using CIK.News.Domain;
using CIK.News.Web.Infrastructure;
using CIK.News.Web.Infrastructure.ActionResults.Admin;
using CIK.News.Web.Infrastructure.MediaItem;
using CIK.News.Web.Infrastructure.ViewModels.Admin.DashBoard;
using CIK.News.Web.Infrastructure.ViewModels.Admin.Persistences;

namespace CIK.News.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class ItemController : BaseController
    {
        private readonly IItemCreatingPersistence _itemCreatingPersistence;
        private readonly IItemEditingPersistence _itemEditingPersistence;
        private readonly IItemDeletingPersistence _itemDeletingPersistence;
        private readonly IMediaItemStorage _itemStorage;

        public ItemController(
            IItemCreatingPersistence itemCreatingPersistence, 
            IItemDeletingPersistence itemDeletingPersistence,
            IItemEditingPersistence itemEditingPersistence,
            IMediaItemStorage itemStorage)
        {
            _itemCreatingPersistence = itemCreatingPersistence;
            _itemDeletingPersistence = itemDeletingPersistence;
            _itemEditingPersistence = itemEditingPersistence;

            _itemStorage = itemStorage;
        }

        public ActionResult Create()
        {
            return new ItemCreatingViewModelActionResult<ItemController>(x => x.Create());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(ItemCreatingViewModel viewModel)
        {
            var item = new Item();

            item.Category.Id = viewModel.CategoryId;
            item.ItemContent.Title = viewModel.Title;
            item.ItemContent.SortDescription = viewModel.SortDescription;
            item.ItemContent.Content = viewModel.Content;
            item.CreatedBy = User.Identity.Name;

            using (var memoryStream = new MemoryStream())
            {
                viewModel.SmallImage.InputStream.CopyTo(memoryStream);

                item.ItemContent.SmallImage = _itemStorage.Storage(memoryStream, viewModel.SmallImage.FileName);
            }

            using (var memoryStream = new MemoryStream())
            {
                viewModel.MediumImage.InputStream.CopyTo(memoryStream);

                item.ItemContent.MediumImage = _itemStorage.Storage(memoryStream, viewModel.MediumImage.FileName);
            }

            using (var memoryStream = new MemoryStream())
            {
                viewModel.BigImage.InputStream.CopyTo(memoryStream);

                item.ItemContent.BigImage = _itemStorage.Storage(memoryStream, viewModel.BigImage.FileName);
            }

            if (_itemCreatingPersistence.PersistenceItem(item))
                SucceedMessage("Save item successfully");
            else
                ErrorMessage("Cannot create item");

            return RedirectToAction("Index", "Dashboard");
        }

        public ActionResult Edit(int id)
        {
            return new ItemEditingViewModelActionResult<ItemController>(x => x.Edit(id), id);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ItemEditingViewModel viewModel)
        {
            var item = new Item();

            item.Id = viewModel.ItemId;
            item.Category.Id = viewModel.CategoryId;
            item.ItemContent.Title = viewModel.Title;
            item.ItemContent.SortDescription = viewModel.SortDescription;
            item.ItemContent.Content = viewModel.Content;
            item.CreatedBy = User.Identity.Name;

            if (viewModel.SmallImage != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    viewModel.SmallImage.InputStream.CopyTo(memoryStream);

                    item.ItemContent.SmallImage = _itemStorage.Storage(memoryStream, viewModel.SmallImage.FileName);
                }
            }

            if (viewModel.MediumImage != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    viewModel.MediumImage.InputStream.CopyTo(memoryStream);

                    item.ItemContent.MediumImage = _itemStorage.Storage(memoryStream, viewModel.MediumImage.FileName);
                }
            }

            if (viewModel.BigImage != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    viewModel.BigImage.InputStream.CopyTo(memoryStream);

                    item.ItemContent.BigImage = _itemStorage.Storage(memoryStream, viewModel.BigImage.FileName);
                }
            }

            if (_itemEditingPersistence.PersistenceItem(item))
                SucceedMessage("Save item successfully");
            else
                ErrorMessage("Cannot edit item");

            return RedirectToAction("Index", "Dashboard");
        }

        public ActionResult Delete(int id)
        {
            var isSucceed = _itemDeletingPersistence.PersistenceItem(id);

            if(isSucceed)
                SucceedMessage("Delete item successfully");
            else
                ErrorMessage("Cannot delete item");

            return RedirectToAction("Index", "Dashboard");
        }
    }
}
